<?php
namespace App\Repository;
use App\Common\Tools;
use App\Exceptions\BusinessException;
use App\Common\ResponseCode;
use App\Models\Account\CustomerAccountRechargeLog;
use App\Models\Base\Config;
use Illuminate\Support\Facades\Log;

/**
 * 系统配置数据仓储类
 *
 * @package App\Http\Repository
 * @author WangBin
 * @mail: <849850352@qq.com>
 */
class SystemConfigRepository extends BaseRepository
{
    /**
     * 保存数据
     *
     * @param array $data
     * @return bool
     * @throws
     * @return int|mixed
     */
    public function save(array $data)
    {
        foreach ($data as $key => $value) {
            Config::query()->updateOrInsert(
                ['config_key' => $key],
                ['config_name' => '参数配置', 'config_key' => $key, 'value' => $value, 'updated_at' => Tools::getDateTime()]
            );
        }
        return true;
    }

    /**
     * 获取系统配置值
     *
     * @param string $configKey
     * @return \Illuminate\Database\Eloquent\HigherOrderBuilderProxy|mixed
     */
    public function get(string $configKey)
    {
        return Config::query()->where('config_key', $configKey)->first()->value;
    }

    /**
     * 获取参数配置列表
     *
     * @param string $prefix
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function getList(string $prefix = '')
    {
        $query = Config::query();
        if(!empty($prefix)) {
            $query->where('config_key', 'like', $prefix . '%');
        }
        return $query;
    }
}
